package routers

import (
	"HTanfang/controllers"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
	"strings"
)

func init() {
	var FilterUser = func(c *context.Context) {
		if c.Request.RequestURI == "/login" {
			return
		}
		if strings.Index(c.Request.RequestURI, "test") != -1 {
			return
		}
		if strings.Index(c.Request.RequestURI, "/devices/list") != -1 || strings.Index(c.Request.RequestURI, "/devicemessages/getlatest") != -1 || strings.Index(c.Request.RequestURI, "/devicemessages/getlist") != -1 {
			return
		}
		_, ok := c.Input.Session("uid").(int)
		if !ok {
			c.Redirect(302, "/login")
		}
	}

	beego.InsertFilter("*", beego.BeforeRouter, FilterUser)
	beego.Router("/", &controllers.MainController{})
	beego.Router("/index_v1.html", &controllers.MainController{}, "*:Index_v1")
	beego.Router("/login", &controllers.MainController{}, "*:Login")
	beego.Router("/logout", &controllers.MainController{}, "*:Logout")

	//Devices
	beego.Router("/devices/list_html/*", &controllers.DevicesController{}, "*:HTDevicesList_html")
	beego.Router("/devices/list", &controllers.DevicesController{}, "*:HTDevicesList")
	beego.Router("/devices/detail", &controllers.DevicesController{}, "*:HTDevicesDetail")
	beego.Router("/devices/add", &controllers.DevicesController{}, "*:HTDevicesAdd")
	beego.Router("/devices/update", &controllers.DevicesController{}, "*:HTDevicesUpdate")
	beego.Router("/devices/delete", &controllers.DevicesController{}, "*:HTDevicesDelete")
	beego.Router("/devices/picUp", &controllers.DevicesController{}, "*:HTDevicesPicUp")
	beego.Router("/devices/chart", &controllers.DevicesController{}, "*:DeviceChar")

	//DeviceMessages
	beego.Router("/devicemessages/list_html/*", &controllers.DeviceMessagesController{}, "*:HTDeviceMessagesList_html")
	beego.Router("/devicemessages/list", &controllers.DeviceMessagesController{}, "*:HTDeviceMessagesList")
	beego.Router("/devicemessages/detail", &controllers.DeviceMessagesController{}, "*:HTDeviceMessagesDetail")
	beego.Router("/devicemessages/add", &controllers.DeviceMessagesController{}, "*:HTDeviceMessagesAdd")
	beego.Router("/devicemessages/update", &controllers.DeviceMessagesController{}, "*:HTDeviceMessagesUpdate")
	beego.Router("/devicemessages/delete", &controllers.DeviceMessagesController{}, "*:HTDeviceMessagesDelete")
	beego.Router("/devicemessages/getlatest", &controllers.DeviceMessagesController{}, "*:HTDeviceGetMessagesLatest")
	beego.Router("/devicemessages/getlist", &controllers.DeviceMessagesController{}, "*:HTDeviceGetMessagesRecent")

	//Users
	beego.Router("/users/list_html/*", &controllers.UsersController{}, "*:HTUsersList_html")
	beego.Router("/users/list", &controllers.UsersController{}, "*:HTUsersList")
	beego.Router("/users/detail", &controllers.UsersController{}, "*:HTUsersDetail")
	beego.Router("/users/add", &controllers.UsersController{}, "*:HTUsersAdd")
	beego.Router("/users/update", &controllers.UsersController{}, "*:HTUsersUpdate")
	beego.Router("/users/delete", &controllers.UsersController{}, "*:HTUsersDelete")

	beego.Router("/test/push", &controllers.MainController{}, "*:PushTest")


}
